<script setup>
    import {ref,computed} from 'vue'
    import { useRouter } from 'vue-router';
    import Product from '@/components/shop/Product.vue';
    // 导入购物车仓库
    import useCartStore from '@/stores/cart.js';
    const router = useRouter()
    const props = defineProps(['shopName'])
    const cartStore = useCartStore()

    // 获取当前店铺的购物车
    const shopCart = computed(()=>{   // ComputedRefImpl

        return cartStore.cartList.find(item => item.name === props.shopName)
    })

    // 加购的商品数量
    const shopCartCount = computed(()=>{

        if(shopCart.value){
            // reduce
            return shopCart.value.list.reduce((sum,item)=>sum + item.number,0)
        }else{
            return 0
        }
    })

    // 获取当前购物车所有的商品
    const shopCartList = computed(()=>{

        if(shopCart.value){
            return shopCart.value.list
        }else{
            return []
        }
    })

    // 弹窗切换
    const isShow = ref(false)

    // 被选中商品列表
    const shopCartCheckedList = computed(()=>{
        console.log('更新')
        return shopCartList.value.filter(item => item.checked)
    })

    // 被选中商品的数量
    const shopCartCheckedCount = computed(()=>{
        return shopCartCheckedList.value.reduce((sum,item)=>sum + item.number,0)
    })

    // 全选全不选
    const checkAll = computed({
        get(){
            // 小控制大
            return shopCart.value.list.every(item => item.checked)
        },
        set(data){ // 当计算属性被修改成其他值时，这个值会被存在data中
            // 大控制小
            shopCart.value.list.forEach(item=>{
                item.checked = data
            })
            
        }
    })   // getter  setter



    // 清空购物车
    const cleanCart = ()=>{
        // 将购物车中所有的商品数量清0
        shopCart.value.list.forEach(item => item.number = 0)

        // 将对应的购物车删除
        cartStore.cartList = cartStore.cartList.filter(item => item.name !== props.shopName)
    }

    // 结算
    const orderConfirm = ()=>{
        // 编程时路由跳转
        router.push({
            path:'/orderConfirm',
            query:{
                shopName:props.shopName
            }
        })
    }






























    
    // let curShopCart = ref({})

    // // 判断是否显示购物车为空
    // const isEmpty = computed(()=>{
    //   // 查看购物车否是存在
    //    curShopCart.value = cartStore.cartList.find(item => item.name === props.shopName)
      
    //   // 存在时
    //   if(curShopCart.value){
    //     // 购物车中存在下标为0的商品
    //     if(curShopCart.value.list.length === 0){
    //       return true
    //     }else{
    //       return false
    //     }
    //   // 购物车不存在时
    //   }else{
    //     return true
    //   }
      
    // })
    

    // // 判断tag是否显示，以及tag的数量
    // const tag = computed(()=>{
    //     console.log(isEmpty.value)
    //     if(!isEmpty.value){
    //         console.log('计算')
    //        return curShopCart.value.list.reduce((sum,item)=>{
    //         return sum + item.number
    //       },0)
    //     }
    // })

</script>

<template>
    <div class="cart">
        <!-- 蒙版/遮罩 -->
        <div class="cart_mask" v-if="isShow" @click="isShow = !isShow"></div>
        <div class="cart_product" v-if="isShow">
            <div class="cart_product_header" v-if="shopCartCount > 0">
                <div class="cart_product_header_allChecked">
                    <input type="checkbox" class="circle-checkbox" v-model="checkAll"> 全选
                </div>
                <div class="cart_product_header_clear" @click="cleanCart">清空购物车</div>
            </div>
            <div class="cart_product_list" v-if="isShow">
                <div class="cart_product_list_item" v-for="item in shopCartList" :key="item.id">
                    <div class="checkbox">
                        <!-- <input type="checkbox" class="circle-checkbox" :checked="item.checked" @change="changeChecked(item)" name="" id=""> -->
                        <input type="checkbox" class="circle-checkbox" v-model="item.checked">
                    </div>
                    <div class="content">
                        <Product min="min" :productInfo="item" :shopName="props.shopName"/>
                    </div>
                </div>
            </div>
        </div>
        <div class="cart_check">
            <div class="cart_check_icon">
                <img src="http://www.dell-lee.com/imgs/vue3/basket.png" alt="" @click="isShow = !isShow">
                <span class="tag" v-if="shopCartCheckedCount > 0">{{ shopCartCheckedCount }}</span>
            </div>
            <div class="cart_check_empty" v-if="shopCartCount === 0">购物车是空的</div>
            <div class="cart_check_content" v-else>
                <div class="cart_check_content_total_price">总计 : <span class="check_content_total_price_number">￥50</span></div>
                <div class="cart_check_content_submit" @click="orderConfirm">结算</div>
            </div>
        </div>
    </div>
</template>

<style lang="scss" scoped>
@import "@/style/viriables.scss";
.cart {
    &_mask {
        position: absolute;
        top: 0;
        bottom: .5rem;
        left: 0;
        right: 0;
        background-color: rgba($color: #000000, $alpha: .4);
    }

    &_product {
        width: 100%;
        position: absolute;
        left: 0;
        bottom: .5rem;
        background-color: $bgColor;

        //圆形复选框
        /* 设置选中背景 */
        .circle-checkbox:checked {
            background: #1673ff;
        }

        /* 设置复选框样式 */
        .circle-checkbox {
            width: .2rem;
            height: .2rem;
            background-color: #ffffff;
            border: solid 1px $light-fontColor;
            -webkit-border-radius: 50%;
            border-radius: 50%;
            font-size: 0.8rem;
            margin: 0;
            padding: 0;
            position: relative;
            display: inline-block;
            vertical-align: top;
            cursor: default;
            -webkit-appearance: none;
            -webkit-user-select: none;
            user-select: none;
        }

        /* 设置伪类，即显现的对勾样式 */
        .circle-checkbox:checked::after {
            content: "";
            top: 5px;
            left: 4px;
            position: absolute;
            background: transparent;
            border: #fff solid 2px;
            border-top: none;
            border-right: none;
            height: 3px;
            width: 8px;
            -moz-transform: rotate(-45deg);
            -ms-transform: rotate(-45deg);
            -webkit-transform: rotate(-45deg);
            transform: rotate(-45deg);
        }

        &_header {
            padding: .18rem;
            display: flex;
            justify-content: space-between;
        }

        &_list {
            max-height: 3rem;
            overflow-y: scroll;

            &_item {
                display: flex;

                .checkbox {
                    display: flex;
                    align-items: center;
                    margin-left: .18rem;  
                }

                .content {
                    flex: 1;
                    width: 100%;
                }
            }
        }
    }

    &_check {
        width: 100%;
        height: .49rem;
        border-top: .01rem solid $content-bgColor;
        position: absolute;
        left: 0;
        bottom: 0;
        display: flex;
        // 购物车图标
        &_icon {
            width: .84rem;
            height: .49rem;
            display: flex;
            justify-content: center;
            align-items: center;
            position: relative;
            img {
                 width: .28rem;
                height: .26rem;
            }

            .tag {
                display: inline-block;
                width: .15rem;
                height: .15rem;
                border-radius: 50%;
                background-color: $hightlight-fontColor;
                color: $bgColor;
                font-size: .1rem;
                text-align: center;
                position: absolute;
                top: .05rem;
                right: .18rem;
            }
           
        }
        // 空购物车
        &_empty {
            line-height: .49rem;
        }
        // 总价
        &_content {
            flex: 1;
            display: flex;
            position: relative;
            width: 100%;

            &_total_price {
                flex: 1;
                line-height: .49rem;
                color: $hightlight-fontColor;

                &_number {
                    font-size: .18rem;
                }
            }
            &_submit {
                width: .98rem;
                height: .49rem;
                background-color: $btn-bgColor;
                color: $bgColor;
                text-align: center;
                line-height: .49rem;
            }
        }
    }
}
</style>